package com.jevons.muffin.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.jevons.muffin.R;

import java.util.List;

/**
 * CustomToolbar 弹出框公用类
 * <p>
 * create at:  2016/4/28
 */
public abstract class AbsPopup {

    // 传入的目标view
    private View anchor;
    // pop视图里的listview
    private ListView listView;
    // pop框
    private PopupWindow popWindow;
    // listview中文本最长的一个text的位置（计算pop框高度的时候需要用到）
    private int maxTextPositon = 0;

    /**
     * @param anchor   pop相对位置的view
     * @param dictList pop框的数据（List形式）
     */
    public AbsPopup(View anchor, List<String> dictList) {
        this.anchor = anchor;
        init(anchor, dictList);
    }

    /**
     * 初始化pop框
     *
     * @param anchor   pop相对位置的view
     * @param dictList pop框数据
     */
    protected void init(View anchor, final List<String> dictList) {
        String[] arrayStr = new String[dictList.size()];
        String temp = "";

        // 得到text最长的item位置（listview用此item的高度作为平均高度，从而得到整个pop的高度）
        for (int i = 0; i < dictList.size(); i++) {
            arrayStr[i] = dictList.get(i);
            if (temp.length() < arrayStr[i].length()) {
                temp = arrayStr[i];
                maxTextPositon = i;
            }
        }

        Context context = anchor.getContext();
        // 记载listview的数据
        this.listView = ((ListView) View.inflate(context, R.layout.popup_title_drop, null));
        this.listView.setPadding(0, 0, 0, 0);
        this.listView.setAdapter(new ArrayAdapter<String>(context, R.layout.item_title_drop_text, arrayStr));
        this.listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parentView, View v, int position, long id) {

                popWindow.dismiss();
                // pop点击回调
                onPopupItemClick(position);
            }
        });

        //pop框的设置
        this.popWindow = new PopupWindow(this.listView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        this.popWindow.setBackgroundDrawable(new BitmapDrawable());
        this.popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                //pop框消失回调
                onPopupDismiss();
            }
        });
    }

    /**
     * 设置pop框的背景
     *
     * @param backgroundDrawable pop框背景文件
     */
    public void setPopupBackground(Drawable backgroundDrawable) {
        this.popWindow.setBackgroundDrawable(backgroundDrawable);
    }

    /**
     * 显示pop框
     */
    public void show() {
        //得到宽度度最宽的item；
        View view = listView.getAdapter().getView(this.maxTextPositon, null, listView);
        view.measure(0, 0);
        //listview的个数
        int count = listView.getAdapter().getCount();
        //item的宽度
        int measuredWidth = view.getMeasuredWidth();
        //item的高度
        int measureHeight = view.getMeasuredHeight();
        //分割线的高度
        int dividerHeight = listView.getDividerHeight();
        //paddingtop
        int padingTop = listView.getPaddingTop();
        //paddingBottom
        int padingBottom = listView.getPaddingBottom();
        //paddingLeft
        int paddingLeft = listView.getPaddingLeft();
        //paddingRight
        int paddingRight = listView.getPaddingRight();

        //pop最高的高度
        int maxHeight = anchor.getContext().getResources().getDisplayMetrics().heightPixels / 3;

        //pop框的宽度
        int width = measuredWidth + paddingLeft + paddingRight;

        if (width < 400) {
            width = 400;
        }

        //设置pop的宽度
        popWindow.setWidth(width);
        //设置pop的高度
        popWindow.setHeight(
                Math.min(count * measureHeight + dividerHeight * (count - 1) + padingTop + padingBottom, maxHeight));

        this.popWindow.showAsDropDown(anchor, 0, 0, Gravity.RIGHT);
    }

    /**
     * pop框点击消失的回调
     */
    protected abstract void onPopupDismiss();

    /**
     * pop框选中item的回调
     *
     */
    protected abstract void onPopupItemClick(int position);
}
